﻿#region Usings

using System;
using System.Runtime.Serialization;
using Rail.Net.Common;

#endregion

namespace Rail.Net.Core.Model.Nonpersistent
{
    /// <summary>
    ///   Represents a station model within a connection/route of the transport plan.
    ///   It contains information like: Eva number, Name, X coordinate, Y coordinate, Departure time, Arrival time
    /// </summary>
    [Serializable]
    [DataContract]
    public class StationModel : IEquatable<StationModel>, IComparable<StationModel>
    {
        #region Constructors

        public StationModel()
        {
            DepartureTime = ArrivalTime = DateTime.Now;
        }

        #endregion

        #region Properties

        /// <summary>
        ///   Id
        /// </summary>
        [DataMember]
        public int Id { get; set; }

        /// <summary>
        ///   Eva number
        /// </summary>
        [DataMember]
        public string Eva { get; set; }

        /// <summary>
        /// Epa number from the data model
        /// </summary>
        public string Epa
        {
            get
            {
                return DataModel != null ? DataModel.Epa : null;
            }
        }

        /// <summary>
        ///   Station name
        /// </summary>
        [DataMember]
        public string Name { get; set; }

        /// <summary>
        ///   X coordinate
        /// </summary>
        [DataMember]
        public string X { get; set; }

        /// <summary>
        ///   Y coordinate
        /// </summary>
        [DataMember]
        public string Y { get; set; }

        /// <summary>
        ///   Departure time
        /// </summary>
        [DataMember]
        public DateTime DepartureTime { get; set; }

        /// <summary>
        ///   Arrival time
        /// </summary>
        [DataMember]
        public DateTime ArrivalTime { get; set; }

        [DataMember]
        public BorderModel BorderModel { get; set; }

        /// <summary>
        ///   True if the station is domestic.
        /// </summary>
        public bool IsDomestic
        {
            get { return Ru == Constants.DomesticRu; }
        }

        /// <summary>
        /// True if the station is international
        /// </summary>
        public bool IsInternational
        {
            get { return !IsDomestic; }
        }

        public bool IsBorder
        {
            get { return BorderModel != null; }
        }

        /// <summary>
        ///   Ru
        /// </summary>
        public string Ru
        {
            get { return Eva != null ? Eva.Substring(0, 2) : null; }
        }

        /// <summary>
        ///   Data model
        /// </summary>
        public Station DataModel { get; set; }

        /// <summary>
        ///   Data model used for distance caluculation (has alternative border station is used if defined)
        /// </summary>
        public Station DataModelForDistance
        {
            get
            {
                if (DataModel != null && DataModel.AlternativeStation != null)
                {
                    return DataModel.AlternativeStation;
                }
                return DataModel;
            }
        }

        #endregion

        #region IEquatable<StationModel> Members

        public bool Equals(StationModel other)
        {
            return Eva == other.Eva;
        }

        #endregion

        #region IComparable<StationModel>

        public int CompareTo(StationModel other)
        {
            return Id.CompareTo(other.Id);
        }

        #endregion
    }
}